clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/***************************/
/* TABLE DIRECTION OF MOVE */
/***************************/
capture program drop regrapid3
program define regrapid3
version 11
syntax, occp(name) 

/* weight inverse of the size of the group in the CZ */
use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 
gen lnbze=ln(zepop)

xi , pre(REG_) i.nreg

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)
keep if lshift==1
/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
gen adimm2=adimm-dimm

/* change immig */
ivreg2 dzeimmig dimm   Y_* REG_* [aweight = iw2] , partial( Y_* REG_*) cl(ze)
estimates store mv0`occp'

/* change immig growth */
ivreg2 adimm2 (dimm = dimm75)  Y_*  REG_* [aweight = iw2] , partial( Y_* REG_*) cl(ze)
estimates store mv1`occp'

/* housing costs */
ivreg2 liloy (dimm = dimtm2)  if an==2007 [aweight = iw2] ,  cl(ze)
estimates store mv3`occp'


use temp.dta, clear
end

capture rm "C:\Users\Public\Documents\Tab21\Tab5_dirmov.rtf"
regrapid3, occp(all)
regrapid3, occp(cad)
regrapid3, occp(tecemp2)
regrapid3, occp(bc)
regrapid3, occp(bcnq)

esttab mv0all mv0cad mv0tecemp2 mv0bc mv0bcnq ///
using "C:\Users\Public\Documents\Tab21\Tab5_dirmov.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
esttab mv1all mv1cad mv1tecemp2 mv1bc mv1bcnq ///
using "C:\Users\Public\Documents\Tab21\Tab5_dirmov.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
esttab mv3all mv3cad mv3tecemp2 mv3bc mv3bcnq ///
using "C:\Users\Public\Documents\Tab21\Tab5_dirmov.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
